package org.jeecg.modules.basic.demo.apartment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.demo.apartment.entity.LwgyBaseApartmentDistribute;
import org.jeecg.modules.basic.demo.apartment.entity.LwgyBaseApartmentDistributeXy;
import org.jeecg.modules.basic.demo.apartment.mapper.LwgyBaseApartmentDistributeMapper;
import org.jeecg.modules.basic.demo.apartment.service.ILwgyBaseApartmentDistributeService;
import org.jeecg.modules.basic.demo.bed.entity.LwgyBaseBed;
import org.jeecg.modules.basic.demo.bed.service.ILwgyBaseBedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 学校公寓信息
 * @Author: sn
 * @Date: 2021-10-25
 * @Version: V1.9
 */
@Service
public class LwgyBaseApartmentDistributeServiceImpl extends ServiceImpl<LwgyBaseApartmentDistributeMapper, LwgyBaseApartmentDistribute> implements ILwgyBaseApartmentDistributeService {
    @Autowired
    private LwgyBaseApartmentDistributeMapper lwgyBaseApartmentDistributeMapper;

    @Autowired
    private ILwgyBaseBedService lwgyBaseBedService;

    public List<LwgyBaseApartmentDistribute> queryDistributeXqList(){
        List<LwgyBaseApartmentDistribute> list=lwgyBaseApartmentDistributeMapper.queryDistributeXqList();
        return list;
    }
    public List<LwgyBaseApartmentDistribute> queryDistributeLhList(String campusId){
        List<LwgyBaseApartmentDistribute> list=lwgyBaseApartmentDistributeMapper.queryDistributeLhList(campusId);
        return list;
    }

    public List<LwgyBaseApartmentDistribute> queryDistributeLhMoreList(String campusId){
        List<LwgyBaseApartmentDistribute> list=lwgyBaseApartmentDistributeMapper.queryDistributeLhList(campusId);
        for (LwgyBaseApartmentDistribute item : list) {
            List<LwgyBaseApartmentDistributeXy> xyList =lwgyBaseApartmentDistributeMapper.queryDistributeXyByLhcode(item.getBuildingId());

            if(oConvertUtils.isNotEmpty(xyList)){
                for(LwgyBaseApartmentDistributeXy m1:xyList){
                    if(oConvertUtils.isNotEmpty(m1.getAdminId())){
                        LwgyBaseApartmentDistributeXy m2 =  lwgyBaseApartmentDistributeMapper.queryDistributeXyByAdminId(m1.getAdminId());
                        m1.setCollegeName(m2.getCollegeName());
                        m1.setCollegeId(m2.getCollegeId());
                        m1.setAdminName(m2.getAdminName());
                    }

                }

            }

            item.setDisxyList(xyList);
         }

       return list;
   }

    public List<LwgyBaseApartmentDistribute> queryDistributeRoomList(String unitId){
        List<LwgyBaseApartmentDistribute> list=lwgyBaseApartmentDistributeMapper.queryDistributeRoomList(unitId);
        return list;
    }
    public List<LwgyBaseApartmentDistribute> queryRoomList(String unitId,String chId){
        System.err.println("调这里了！！！！");
        //List<LwgyBaseApartmentDistribute> fjList=lwgyBaseApartmentDistributeMapper.queryRoomList(unitId,chId);//没有功能房
        List<LwgyBaseApartmentDistribute> fjList=lwgyBaseApartmentDistributeMapper.queryAllRoomList(unitId,chId);//所有房间包括功能房
        List<LwgyBaseApartmentDistribute> infoList=lwgyBaseApartmentDistributeMapper.queryRoomList2(unitId,chId);
        List<LwgyBaseApartmentDistribute> infoXlList=lwgyBaseApartmentDistributeMapper.queryRoomXlList(unitId,chId);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> role = lwgyBaseBedService.getRoleById(sysUser.getId());


        if (!role.contains("admin")) {//不是管理员，显示管理员分配的的床位
            infoList = infoList.stream().filter(item->(sysUser.getSelectedSchools()).equals(item.getSelectedSchools())).collect(Collectors.toList());
        }
        if (role.contains("yanjiusheng")) {//不是管理员，显示管理员分配的的床位
            System.err.println("调这里了吗！99");
            infoXlList = infoXlList.stream().filter(item->(sysUser.getSelectedEducation()).equals(item.getSelectedEducation())).collect(Collectors.toList());
        }
        List<LwgyBaseApartmentDistribute> finalLists = infoList;
        fjList.stream().forEach(item->{
            List<LwgyBaseApartmentDistribute> list = finalLists.stream().filter(item2 -> item2.getRoomId().equals(item.getRoomId())).collect(Collectors.toList()) ;
            item.setDisRoomList(list);
            item.setBedNum(list.size()+"");
        });

        return fjList;
    }

    public List<LwgyBaseApartmentDistributeXy> queryDistributeFdyList(String campusId,String collegeId){
        List<LwgyBaseApartmentDistributeXy> list=lwgyBaseApartmentDistributeMapper.queryDistributeFdyList(campusId,collegeId);
        return list;
    }

    public List<LwgyBaseApartmentDistribute> queryDistributeLhByFdyList(List<String> adminId){
        List<LwgyBaseApartmentDistribute> list=lwgyBaseApartmentDistributeMapper.queryDistributeLhByFdyList(adminId);
        return list;
    }
    public List<LwgyBaseApartmentDistribute> queryDistributeRoomInfoByFdyList(String adminId,String buildingId){
        List<LwgyBaseApartmentDistribute> list=lwgyBaseApartmentDistributeMapper.queryDistributeRoomInfoByFdyList(adminId,buildingId);
        return list;
    }
    public List<String> queryDistributeFdyInfoByCollegeIdList(String collegeId){
        List<String> list=lwgyBaseApartmentDistributeMapper.queryDistributeFdyInfoByCollegeIdList(collegeId);
        return list;
    }

}
